package com.xinyue.game.service.framwork.common;

import com.alibaba.fastjson.JSONObject;
import com.xinyue.game.common.IServerError;
import org.slf4j.helpers.MessageFormatter;

public class GameServerException extends RuntimeException {
    private IServerError serverError;
    private String errorDesc;

    public GameServerException(IServerError error, String formatMsg, Object... args) {
        super(formatErrorMsg(error, formatMsg, args));
        this.serverError = error;
    }

    private static String formatErrorMsg(IServerError error, String formateMsg, Object... args) {
        JSONObject errorData = new JSONObject();
        errorData.put("errorCode", error.getErrorCode());
        String errorMsg;
        if (formateMsg != null) {
            errorMsg = MessageFormatter.arrayFormat(formateMsg, args).getMessage();
        } else {
            errorMsg = error.getErrorMsg();
        }
        errorData.put("errorMsg", errorMsg);
        return errorData.toJSONString();
    }

    public int getErrorCode() {
        return this.serverError.getErrorCode();
    }

    public String getErrorDesc() {
        return this.errorDesc;
    }
}
